package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.page.CommonQuery;
import com.ruoyi.common.utils.StatusConst;
import com.ruoyi.system.mapper.HomeBannerMapper;
import com.ruoyi.system.service.IHomeBannerService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author linxz
 * @since 2025-01-02
 */
@Service
public class HomeBannerServiceImpl extends ServiceImpl<HomeBannerMapper, HomeBanner> implements IHomeBannerService {

    @Override
    public List<HomeBanner> getList(CommonQuery commonQuery) {
        QueryWrapper<HomeBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(commonQuery.getStatus()),"status",commonQuery.getStatus());
        queryWrapper.eq(ObjectUtils.isNotEmpty(commonQuery.getCategory()),"category",commonQuery.getCategory());
        queryWrapper.eq(ObjectUtils.isNotEmpty(commonQuery.getSectionName()),"section_name",commonQuery.getSectionName());
        queryWrapper.eq(ObjectUtils.isNotEmpty(commonQuery.getLocate()),"locate",commonQuery.getLocate());
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public int addEntity(HomeBanner homeBanner) {
        return baseMapper.addEntity(homeBanner);
    }

    @Override
    public int updateEntity(HomeBanner homeBanner) {
        return baseMapper.updateEntity(homeBanner);
    }

    @Override
    public int delete(CommonDelete commonDelete) {
        return baseMapper.delete(commonDelete.getId());
    }

    @Override
    public List<BannerEntity> getBannerTree() {
        List<BannerEntity> categoryEntityList=new ArrayList<>();

        List<String> categoryList=baseMapper.selectCategoryList();
        for (String categoryName:categoryList) {
            BannerEntity categoryEntity=new BannerEntity();
            categoryEntity.setLabel(categoryName);
            categoryEntity.setValue(categoryName);

            // 分类下的所有模块
            List<BannerEntity> sectionEntityList=new ArrayList<>();
            // 根据分类查询模块列表
            List<String> sectionList=baseMapper.selectSectionListByCategory(categoryName);
            for (String sectionName: sectionList) {
                BannerEntity sectionEntity=new BannerEntity();
                sectionEntity.setLabel(sectionName);
                sectionEntity.setValue(sectionName);
                sectionEntityList.add(sectionEntity);

            }
            categoryEntity.setChildren(sectionEntityList);

            categoryEntityList.add(categoryEntity);
        }

        return categoryEntityList;
    }

    @Override
    public List<HomeBanner> getListBySection(QueryBanner queryBanner) {
        return baseMapper.getListBySection(queryBanner.getCategory(),queryBanner.getSectionName());

    }

    @Override
    public List<String> getBannerSectionList() {
        return baseMapper.getBannerSectionList();

    }

    @Override
    public List<Integer> getBannerIdList(QueryBanner query) {
        return baseMapper.getBannerIdList(query.getSectionName());
    }

    @Override
    public HomeBanner getBannerById(int id) {
//        return baseMapper.getBannerById(id);
        return null;

    }

    @Override
    public List<HomeBanner> listByPc(CommonQuery commonQuery) {
        QueryWrapper<HomeBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", StatusConst.NORMAL);
        queryWrapper.eq(ObjectUtils.isNotEmpty(commonQuery.getCategory()),"category",commonQuery.getCategory());
        queryWrapper.eq(ObjectUtils.isNotEmpty(commonQuery.getSectionName()),"section_name",commonQuery.getSectionName());
        queryWrapper.eq(ObjectUtils.isNotEmpty(commonQuery.getLocate()),"locate",commonQuery.getLocate());
        return baseMapper.selectList(queryWrapper);
    }


}
